/*
 * Modbus slave mode data control.
 * Copyright (c) 2012  everyone
 * All rights reserved.
 * no limite
 */
#include "mbdatacb.h"
#include "mbinputdatacfg.h"


uint16_t reg_hold_read(uint16_t Index);
uint16_t reg_hold_write(uint16_t Index, uint16_t Val);
extern const unsigned short mb_hold_dict[];
uint16_t io_out[16];
uint16_t io_in[16];
/*******************************************************************************
 * @brief  reg_hold_read
 * \param[in] Index : data address
 * @return  data
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint16_t reg_hold_read(uint16_t Index)
{
	word_tt Val;
	if (Index < 16)
	{
		Val.uval = io_out[Index];
	}
	else if (Index < 32)
	{
		Val.uval = io_in[Index-32];
	}
	else
	{
		Val.uval = 0;
	}
	return Val.uVal;
}

/*******************************************************************************
 * @brief  reg_hold_write
 * \param[in] Index : data address
 * \param[in] Val : data
 * @return  data
 * @ Pass/ Fail criteria: none
 *******************************************************************************/
uint16_t reg_hold_write(uint16_t Index, uint16_t Val)
{
	if (Index < 16)
	{
		io_out[Index]= Val;
	}
	
	if(Index>=256 && Index < 512)
	{
		uu16 s,p;
		s = (Index-256)/16;
		p = (Index-256)%16;
		
		if(Val)
			io_out[s] |= 1<<p;
		else
			io_out[s] &= ~(1<<p);
		
	}
	return Val;
}
